package thread;

import java.io.*;

/**
 * Created by yangluhua on 2016/9/29 0029.
 */
public class RandomReadFileLineUtils {
    /**
     * 读取从第start行到第finish行的文本数据
     * @param nameFile
     * @param start
     * @param finish
     * @throws Exception
     */
    public static void dataReader(String nameFile, int start, int finish) throws Exception {
        if (start > finish) {
            System.out.println("Error start or finish!");
            return;
        }
        InputStream inputStream = null;
        LineNumberReader reader = null;
        try {
            inputStream = new FileInputStream(new File(nameFile));
            reader = new LineNumberReader(
                    new InputStreamReader(inputStream));
            int lines = getTotalLines(new File(nameFile));
            if (start < 0 || finish < 0 || finish > lines || start > lines) {
                System.out.println("Line not found!");
                return;
            }

            String line = reader.readLine();
            lines = 0;
            while (line != null) {
                lines++;
                if (lines >= start && lines <= finish) {
                    System.out.println(line);
                }
                line = reader.readLine();
            }
            inputStream.close();
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("IO Error");
            System.exit(0);
        }

    }

    /**
     * 读取从第start行到最后的文本数据
     * @param nameFile
     * @param start
     * @throws Exception
     */
    public static void dataReader(String nameFile, int start) throws Exception {
        InputStream inputStream = null;
        LineNumberReader reader = null;
        try {
            inputStream = new FileInputStream(new File(nameFile));
            reader = new LineNumberReader(
                    new InputStreamReader(inputStream));
            int totalLines = getTotalLines(new File(nameFile));
            if (start < 0 || start > totalLines) {
                System.out.println("Line not found!");
                return;
            }

            String line = reader.readLine();
            int lines = 0;
            while (line != null) {
                lines++;
                if (lines >= start && lines <= totalLines) {
                    System.out.println(line);
                }
                line = reader.readLine();
            }
            inputStream.close();
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("IO Error");
            System.exit(0);
        }

    }

    /**
     * 获取文本文件的总行数
     * @param file
     * @return
     * @throws IOException
     */
    private static int getTotalLines(File file) throws IOException {
        FileReader in = new FileReader(file);
        LineNumberReader reader = new LineNumberReader(in);
        String line = reader.readLine();
        int lines = 0;
        while (line != null) {
            lines++;
            line = reader.readLine();
        }
        reader.close();
        in.close();
        return lines;
    }
}
